import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {

    saveStoragelocation, //保存
    getStoragelocationByFid, //详情


} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "showName"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            fCompanyids: null,
            fCompanynames: null,
            btnStatus: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            currntData: null,
            dataInfo: null,
            startDates: null,
            treeData: [], //材料种类
            formData: {
                fId: "", //传fid表示修改 不传表示新增
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                fProjectid: "",
                fProjectname: "",
                fCompanyid: "", //左侧选中组织机构id
                fCompanyname: "",
                fCreatedate: "",
                fCreateuserid: "",
                fCreateusername: "",
                fLocationname: "", //存放位置
                fSort: "", //排序号
                fIsdeleted: "",
                fReportid: "", //操作人id
                fReportname: "", //操作人姓名
                fNote: "", //备注
                fReportdates: this.common.day(0) //添加日期
            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentDetail: null,
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        })
                    }
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanyids = newVal

                }
            },
            immediate: true,
        },
        showName: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
        this.btnShow = true

        if (this.statusNum == 1) { //新增
            this.formData.fCompanyname = this.fCompanynames; //当前登录人标段名称
            this.formData.fCompanyid = this.fCompanyids; //当前登录人标段ID
            this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formData.fCreateuserid = this.userInfo.userId; //当前登录人id
            this.formData.fCreateusername = this.userInfo.realName; //当前登录人名称
            this.formData.fProjectid = this.userInfo.projectId; //当前登录人
            this.formData.fProjectname = this.userInfo.projectName; //当前登录人
            this.formData.fCompanyid = this.fCompanyids; //左侧选中组织机构id
            this.formData.fCompanyname = this.fCompanynames; //左侧选中组织机构


        } else if (this.statusNum == 2) { //编辑
            // currentRow
            this.formData.fCompanyname = this.currentDetail.fCompanyname; //当前登录人标段名称
            this.formData.fCompanyid = this.currentDetail.fCompanyid; //当前登录人标段ID
            this.formData.fId = this.currentDetail.fId //按钮 
            this.getDetail({
                fId: this.currentDetail.fId
            })

            this.formObj.map((item) => {
                item.disabled = false;
            });
        } else if (this.statusNum == 7) { //查看
            this.getDetail({
                fId: this.currentDetail.fId
            })
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.3 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange() {},
        //   edit(status)
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },

        // 详情
        getDetail(obj) {
            this.loading = true;
            getStoragelocationByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpClStoragelocation
                    this.formData = data
                    if (this.statusNum == 7) {
                        this.btnShow = false
                        this.formObj.map((item) => {
                            item.disabled = true;
                        });
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },

        yj_saveForm() {
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data) {
            this.loading = true;

            this.isLock = true
            this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));

            
            this.formData.fProjectname = this.fShortname; //当前登录人标段名称
            this.formData.fProjectid = this.fProjectid; //当前登录人标段ID
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            this.formData.fReportid = this.userInfo.userId; //当前登录人id
            this.formData.fReportname = this.userInfo.realName; //当前登录人名称
            saveStoragelocation(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;

                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;

                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })


        },
    },
};